package com.cx.java;

import java.io.IOException;
import java.util.TreeSet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AddServlet
 */
@WebServlet("/AddServlet")
public class AddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		// TODO Auto-generated method stub
	//获取添加页面中的数据
		String carName = request.getParameter("carname");
		String carLoadtime = request.getParameter("carLoadtime");
		String mileage = request.getParameter("mileage");
		String volume = request.getParameter("volume");
		String gearbox = request.getParameter("gearbox");
		String price = request.getParameter("price");
		String img = request.getParameter("img");
		
		//连接数据库
		JdbcUtils ju = JdbcUtils.getInstance();
		/*
		 * request.setCharacterEncoding("UTF-8");
		 * response.setContentType("text/html;charset=UTF-8");
		 * response.setCharacterEncoding("UTF-8");
		 */
		String sql = "insert into car(carName,carLoadtime,mileage,volume,gearbox, price,img) "
				+ "values('"+carName+"','"+carLoadtime+"','"+mileage+"','"+volume+"','"+gearbox+"','"+price+"','"+img+"');";
		System.out.println(sql);
		
		
		
		int affectedRow = ju.executeUpdate(sql);
		if(affectedRow > 0) {
			
			// 成功- 调到展示页面（添加页面待定）
			response.sendRedirect("/car/CarInfoServlet");
			
		} else {
			// 失败，添加失败返回添加页面			
			response.sendRedirect("/car/add.jsp");
		}

	}
	


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
